{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False, False, False, False, False, False]\n",
      "1 : 1\n",
      "2 : 2\n",
      "3 : 3\n",
      "4 : 4\n",
      "5 : 5\n",
      "6 : 1\n",
      "1 : 2\n",
      "2 : 3\n",
      "3 : 4\n",
      "4 : 5\n",
      "6 : 1\n",
      "1 : 2\n",
      "2 : 3\n",
      "3 : 4\n",
      "6 : 5\n",
      "1 : 1\n",
      "2 : 2\n",
      "3 : 3\n",
      "1 : 4\n",
      "2 : 5\n",
      "3 : 1\n",
      "1 : 2\n",
      "3 : 3\n",
      "1 : 4\n",
      "3 : 5\n",
      "1 : 1\n",
      "1 : 2\n",
      "1 : 3\n",
      "1 : 4\n",
      "1 : 5\n",
      "[5, 4, 6, 2, 3, 1]\n"
     ]
    }
   ],
   "source": [
    "# 5章 第6题\n",
    "\n",
    "def Josephus(N,M):\n",
    "    # 使用布尔列表标记每个人状态\n",
    "    people = [False]*N\n",
    "    print(people)\n",
    "\n",
    "    # 淘汰列表\n",
    "    out_people = []\n",
    "\n",
    "    # 保存报数\n",
    "    count = 0\n",
    "\n",
    "    # 循环报数 报到M 就淘汰，直到所有人被淘汰\n",
    "    while len(out_people)<N:\n",
    "        for i in range(N):\n",
    "            # 如果这个人还没有被淘汰，报数\n",
    "            if people[i] == False:\n",
    "                count += 1\n",
    "                print(i+1,\":\",count)\n",
    "                # 如果报到M\n",
    "                if count==M:\n",
    "                    # 这个人淘汰\n",
    "                    people[i] = True\n",
    "                    out_people.append(i+1)\n",
    "                    # 重新开始报数\n",
    "                    count = 0\n",
    "    # 打印淘汰列表\n",
    "    print(out_people)\n",
    "\n",
    "Josephus(6,5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
